/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.action;


import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;

import kiff.entity.User;
import kiff.interceptor.UserAware;
import kiff.service.CoreService;

/**
 * Abstract CRUD action.  This class implements routine CRUD operations.
 * @author Adam
 * @version $Id$
 * @param <T> The type of model the CRUD action will operate on. 
 * 
 * Created on Nov 9, 2008 at 2:38:18 PM 
 */
public abstract class AbstractCRUDAction<T> extends BaseAction implements UserAware {
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = 4118859853335785753L;

	/**
	 * The action to submit to.
	 */
	private String actionMethod;

	/**
	 * The destination of result type.
	 */
	private String destination;

	/**
	 * A List of entities of type T, used when executing the list method.
	 */
	protected List<T> list;

	/**
	 * The logged in user.
	 */
	protected User securityUser;

	/**
	 * Service used to execute business logic.
	 */
	protected CoreService<T> service;

	/**
	 * Prepares the page for creating a new entity.
	 * @return ADD.
	 */
	@SkipValidation
	public String add() {
		setActionMethod(CREATE);
		return ADD;
	}

	/**
	 * Creates a new entity.
	 * @return LIST.
	 */
	public String create() {
		service.insert(getModel(), securityUser);
		return list();
	}

	/**
	 * Prepares the page for updating an existing entity.
	 * @return EDIT.
	 */
	@SkipValidation
	public String edit() {
		setActionMethod(UPDATE);
		return EDIT;
	}

	/**
	 * Returns actionMethod.
	 * @return the actionMethod.
	 */
	public String getActionMethod() {
		return actionMethod;
	}

	/**
	 * Returns destination.
	 * @return the destination.
	 */
	public String getDestination() {
		return destination;
	}

	/**
	 * Returns list.
	 * @return the list.
	 */
	public List<T> getList() {
		return list;
	}

	/**
	 * Returns the requested model.
	 * @return the requested model.
	 */
	public abstract T getModel();

	/**
	 * Prepares the page to display a list of entities.
	 * @return LIST.
	 */
	@SkipValidation
	public String list() {
		list = service.findAll();
		return LIST;
	}

	/**
	 * Removes an entity.
	 * @return LIST.
	 */
	@SkipValidation
	public String remove() {
		service.remove(getModel(), securityUser);
		return list();
	}

	/**
	 * Sets actionMethod.
	 * @param actionMethod the actionMethod to set.
	 */
	public void setActionMethod(final String actionMethod) {
		this.actionMethod = actionMethod;
	}

	/**
	 * Sets destination.
	 * @param destination the destination to set.
	 */
	public void setDestination(final String destination) {
		this.destination = destination;
	}

	/**
	 * Sets service.
	 * @param service the service to set.
	 */
	public abstract void setService(CoreService<T> service);

	/**
	 * {@inheritDoc}
	 * @see kiff.interceptor.UserAware#setUser(kiff.entity.User)
	 */
	public void setUser(final User user) {
		this.securityUser = user;
	}

	/**
	 * Prepares the page for a single entity for a detail display.
	 * @return SHOW.
	 */
	@SkipValidation
	public String show() {
		return SHOW;
	}

	/**
	 * Updates an entity.
	 * @return LIST.
	 */
	public String update() {
		service.update(getModel(), securityUser);
		return list();
	}

}
